package com.wechat.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wechat.admin.mapper.UserDeviceLinkMapper;
import com.wechat.admin.model.entity.User;
import com.wechat.admin.model.entity.UserDeviceLink;
import com.wechat.admin.model.entity.WeDevice;
import com.wechat.admin.model.request.GetUserAndDeviceInfoRequest;
import com.wechat.admin.model.vo.DeviceVo;
import com.wechat.admin.model.vo.UserLoginInfoVo;
import com.wechat.admin.service.UserDeviceLinkService;
import com.wechat.admin.service.WeDeviceService;
import org.apache.poi.hssf.record.DVALRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author dell
* @description 针对表【user_device_link】的数据库操作Service实现
* @createDate 2024-12-15 21:04:20
*/
@Service
public class UserDeviceLinkServiceImpl extends ServiceImpl<UserDeviceLinkMapper, UserDeviceLink>
    implements UserDeviceLinkService {

    @Resource
    private WeDeviceService weDeviceService;
    @Autowired
    private UserServiceImpl userServiceImpl;

    @Override
    public Page<DeviceVo> getUserDeviceLinkByUserId(GetUserAndDeviceInfoRequest getUserAndDeviceInfoRequest) {
        int pageSize = getUserAndDeviceInfoRequest.getPageSize();
        int current = getUserAndDeviceInfoRequest.getCurrent();
        Long userId = getUserAndDeviceInfoRequest.getUserId();

        // 查询设备关联表的所有设备id
        QueryWrapper<WeDevice> weDeviceQueryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<UserDeviceLink> lambdaQueryWrapper = Wrappers.lambdaQuery(UserDeviceLink.class)
                .select(UserDeviceLink::getDeviceId)
                .eq(UserDeviceLink::getUserid, userId)
                .eq(UserDeviceLink::getStatus, 1);

        List<UserDeviceLink> userDeviceLinks = this.list(lambdaQueryWrapper);

        if (CollUtil.isNotEmpty(userDeviceLinks)) {
            // 取出所有的设备id
            List<String> deviceIdList = userDeviceLinks.stream().map(UserDeviceLink::getDeviceId)
                    .collect(Collectors.toList());
            weDeviceQueryWrapper.in("deviceid", deviceIdList);
        }else{
            weDeviceQueryWrapper.isNull("deviceid");
        }
        Page<WeDevice> weDevicePage = weDeviceService.page(new Page<>(current, pageSize), weDeviceQueryWrapper);
        return convertToDeviceVoPage(weDevicePage);
    }

    public Page<DeviceVo> convertToDeviceVoPage(Page<WeDevice> weDevicePage) {
        Page<DeviceVo> deviceVoPage = new Page<>(
                weDevicePage.getCurrent(),
                weDevicePage.getSize(),
                weDevicePage.getTotal());

        // 转换 records 列表，将 WeDevice 转换为 DeviceVo
        List<DeviceVo> deviceVos = weDevicePage.getRecords().stream().map(weDevice -> {
            DeviceVo deviceVo = new DeviceVo();
            deviceVo.setDeviceId(weDevice.getDeviceId());
            deviceVo.setDeviceName(weDevice.getDeviceName());
            deviceVo.setStatus(weDevice.getStatus());
            deviceVo.setCreateTime(weDevice.getCreateTime());
            deviceVo.setToken(weDevice.getToken());
            deviceVo.setLoginTime(weDevice.getLoginTime());
            return deviceVo;
        }).collect(Collectors.toList());
        deviceVoPage.setRecords(deviceVos);
        return deviceVoPage;
    }
}




